package com.ruoyi.webs.utils;


import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ruoyi.common.core.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping({"/code"})
public class CodeController
        extends BaseController {
    @RequestMapping({"/getcode"})
    public void getCode(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();


        HttpSession session = request.getSession();
        session.setAttribute("code", codeMap.get("code").toString());


        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1L);

        response.setContentType("image/jpeg");


        try {
            ServletOutputStream sos = response.getOutputStream();
            ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", (OutputStream) sos);
            sos.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}